package com.utils.info.util.hhy;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

public class NumberUtil {

    /**
     * 加法
     * @param number1
     * @param number2
     * @return
     */
    public static double add(double number1,double number2){
        BigDecimal bignum1 = new BigDecimal(String.valueOf(number1));
        BigDecimal bignum2 = new BigDecimal(String.valueOf(number2));
        BigDecimal bignum3 = bignum1.add(bignum2);
        double result = bignum3.doubleValue();
        return result;
    }


    /**
     * 减法
     * @param number1
     * @param number2
     * @return
     */
    public static double subtract(double number1,double number2){
        BigDecimal bignum1 = new BigDecimal(String.valueOf(number1));
        BigDecimal bignum2 = new BigDecimal(String.valueOf(number2));
        BigDecimal bignum3 = bignum1.subtract(bignum2);
        double result = bignum3.doubleValue();
        return result;
    }


    /**
     * 乘法
     * @param number1
     * @param number2
     * @return
     */
    public static double multiply(double number1,double number2){
        BigDecimal bignum1 = new BigDecimal(String.valueOf(number1));
        BigDecimal bignum2 = new BigDecimal(String.valueOf(number2));
        BigDecimal bignum3 = bignum1.multiply(bignum2);
        double result = bignum3.doubleValue();
        return result;
    }

    /**
     * 除法(保留小数位数)
     * @param number1
     * @param number2
     * @return
     */
    public static double divide(double number1,double number2,int scale){
        BigDecimal bignum1 = new BigDecimal(String.valueOf(number1));
        BigDecimal bignum2 = new BigDecimal(String.valueOf(number2));
        if(number2==0){
            return 0;
        }
        double result = bignum1.divide(bignum2,scale,BigDecimal.ROUND_HALF_UP).doubleValue();
        return result;
    }

    /**
     * 返回百分比
     * @param number1
     * @param number2
     * @param scale
     * @return
     */
    public static double divideRate(double number1,double number2,int scale){
        BigDecimal bignum1 = new BigDecimal(String.valueOf(number1));
        BigDecimal bignum2 = new BigDecimal(String.valueOf(number2));
        if(number2==0){
            return 0;
        }
        double result = formatDouble(bignum1.divide(bignum2,scale,BigDecimal.ROUND_HALF_UP).doubleValue()*100,2);
        return result;
    }


    /**
     * 格式化小时四舍五入保留几位
     * @param number
     * @param scale 保留几位
     * @return
     */
    public static double formatDouble(double number,int scale){
        BigDecimal bigDecimal= new BigDecimal(String.valueOf(number));
        return bigDecimal.setScale(scale,BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    /**
     * 多个值求平均
     * @param list
     * @return
     */
    public static double divideForList(List<Double> list){
        Double resultAvg = list.stream().collect(Collectors.averagingDouble(item -> item));
        return resultAvg;
    }

    /**
     * 使double为整数时不带小数点,为小数时保持小数点
     * @param d
     * @return
     */

    public static String doubleTrans(double d){
        if(Math.round(d)-d==0){
            return String.valueOf((long)d);

        }
        return String.valueOf(d);
    }
}
